package leetcode.editor.cn;

//给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。 
//
// 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。 
//
// 
//
// 
//
// 示例 1： 
//
// 
//输入：digits = "23"
//输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
// 
//
// 示例 2： 
//
// 
//输入：digits = ""
//输出：[]
// 
//
// 示例 3： 
//
// 
//输入：digits = "2"
//输出：["a","b","c"]
// 
//
// 
//
// 提示： 
//
// 
// 0 <= digits.length <= 4 
// digits[i] 是范围 ['2', '9'] 的一个数字。 
// 
// Related Topics 哈希表 字符串 回溯 👍 1852 👎 0

import java.util.*;

//Java：【17】 - 电话号码的字母组合
public class LetterCombinationsOfAPhoneNumber_17{
    public static void main(String[] args) {
        Solution solution = new LetterCombinationsOfAPhoneNumber_17().new Solution();
                // TO TEST
    }
    
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
        public List<String> letterCombinations(String digits) {
            List<String> res = new ArrayList<>();
            if(digits.length()==0) return res;
            int[][] cases = new int[10][2];
            for (int i = 2; i < 10; i++) {
                if(i==2) cases[i][0] = 'a';
                else cases[i][0] = cases[i-1][0]+cases[i-1][1];
                if(i==7 || i==9) cases[i][1] = 4;
                else cases[i][1] = 3;
            }
            Queue<StringBuffer> queue = new LinkedList<>();
            int ch = digits.charAt(0)-'0';
            for (int i = 0; i < cases[ch][1]; i++) {
                queue.offer(new StringBuffer(String.valueOf((char) (cases[ch][0]+i))));
            }
            int len = digits.length();
            for (int i = 1; i <len; i++) {
                int size = queue.size();
                for (int j = 0; j < size; j++) {
                    ch = digits.charAt(i)-'0';
                    for (int k = 0; k < cases[ch][1]; k++) {
                        StringBuffer tmp = new StringBuffer(queue.peek());
                        tmp.append((char)(cases[ch][0]+k));
                        queue.offer(tmp);
                    }
                    queue.remove();
                }
            }
            while(!queue.isEmpty()){
                res.add(queue.poll().toString());
            }
            return res;
        }
}
//leetcode submit region end(Prohibit modification and deletion)

    public List<String> letterCombinations(String digits) {
        List<String> res =new ArrayList<>();
        if(digits.length()==0) return res;
        Map<Character,String> cases = new HashMap<>();
        cases.put('2',"abc");cases.put('3',"def");
        cases.put('4',"ghi");cases.put('5',"jkl");cases.put('6',"nmo");
        cases.put('7',"pqrs");cases.put('8',"tuv");cases.put('9',"wxyz");
        dfs(res,cases,digits,0,new StringBuffer());
        return res;
    }

    public void dfs(List<String> res, Map<Character, String> cases, String digits, int index, StringBuffer buffer){
        if(index == digits.length())
            res.add(buffer.toString());
        else{
            char ch = digits.charAt(index);
            String letter = cases.get(ch);
            int len = letter.length();
            for (int i = 0; i < len; i++) {
                buffer.append(letter.charAt(i));
                dfs(res,cases,digits,index+1,buffer);
                buffer.deleteCharAt(index);
            }
        }
    }

}
